<?php
/*------------------------------------------------------------------------
# mod_vportfolio - VPortfolio Module
# ------------------------------------------------------------------------
# author    Vsmart Extensions
# copyright Copyright (C) 2010 www.vsmart-extensions.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.vsmart-extensions.com
# Technical Support:  Forum - http://www.vsmart-extensions.com
-------------------------------------------------------------------------*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
class ImageHelper
{	
	function __construct() {}
	function &getInstance ()    
    {
        static $instance;        
        if (!isset($instance)) {
            
            $instance = new ImageHelper();
        }         
        return $instance;        
    } 
	/**
	 * Checks if the file is an image
	 * @param string The filename
	 * @return boolean
	 */
	function isImage( $fileName )
	{
		static $imageTypes = 'gif|jpg|jpeg|png|bmp';
		return preg_match("/$imageTypes/i",$fileName);
	}
	/**
	 * Checks if the file is an image
	 * @param string The filename
	 * @return boolean
	 */
	function getTypeIcon( $fileName )
	{
		// Get file extension
		return strtolower(substr($fileName, strrpos($fileName, '.') + 1));
	}
	/*
	*/
	function create_crop($source, $destination, $extension, $crop_width, $crop_height)
	{	
		$size=getimagesize($source);
		$width=$size[0];
		$height=$size[1];
		if ($width >= $crop_width && $height >= $crop_height)
		{
		}
		else if ($width > $crop_width && $height < $crop_height)
		{
			$crop_height = $height;
		}
		else if ($width < $crop_width && $height > $crop_height)
		{
			$crop_width = $width;
		}
		else 
		{
			$crop_height = $height;
			$crop_width = $width;			
		}
		$x=0; $y=0;
		$new_image=imagecreatetruecolor($crop_width,$crop_height) or die();		
		if($extension=="jpg" || $extension=="jpeg")
			$image=imagecreatefromjpeg($source);
		if($extension=="gif")
			$image=imagecreatefromgif($source);
		if($extension=="png")
			$image=imagecreatefrompng($source);
		// crop 
		@imagecopy($new_image,$image,0,0,$x,$y,$crop_width,$crop_height);		
		if($extension=="jpg" || $extension=="jpeg")
			imagejpeg($new_image,$destination);
		if($extension=="gif")
			imagegif($new_image,$destination);
		if($extension=="png")
			imagepng($new_image,$destination);
	}		
	/*
	*/
	function test_W_H_Image($source, $w, $h)
	{	
		$size=getimagesize($source);
		$width=$size[0];
		$height=$size[1];
		if ($width != w && $height != $h)
		{
			$this->deleteImage($source);
			return false;
		}		
		else 
		{
			return true;		
		}		
	}	
	/*
	*/
	function deleteImage($imagePath)
	{
		if(file_exists($imagePath))  
			unlink($imagePath);	
		else 
			return false;		
		return true;		
	}
}
?>

